//
//  FriendStatus.m
//  friendCyc
//
//  Created by sun on 13-3-23.
//  Copyright (c) 2013年 sun. All rights reserved.
//

#import "FriendStatus.h"

#import "FirCirCommentDataSource.h"

#import "FirCirCommentInfo.h"

@implementation FriendStatus

- (id)init
{
    self = [super init];
    if (self) {
        self.mail = @"";
        self.strtime = @"";
        self.stIndex = FCG_ONE_TEXT;
        self.imageIDArray = [[NSMutableArray alloc] init];
        self.commentArr = [[NSMutableArray alloc] init];
        self.ipackuptype = PPT_NULL;
    }
    return self;
}

-(NSString*)TurnTimeString
{
    NSArray *array = [self.strtime componentsSeparatedByString:@"/"];
    
    if ([array count] != 6) {
        return @"";
    }
    
    NSString *stryear = [array objectAtIndex:0];
    int year = [stryear intValue];
    
    NSString *time = @"";
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDate *curdate = [NSDate date];
    comps = [calendar components:unitFlags fromDate:curdate];
    int curyear=[comps year];
    
    if (curyear != year) {
        time = [NSString stringWithFormat:@"%d年",year];
    }
    else
    {
        stryear = [array objectAtIndex:1];
        year = [stryear intValue];
        
        time = [NSString stringWithFormat:@"%@%d月",time,year];
        
        stryear = [array objectAtIndex:2];
        year = [stryear intValue];
        
        time = [NSString stringWithFormat:@"%@%d日",time,year];
        
        stryear = [array objectAtIndex:3];
        year = [stryear intValue];
        
        if (year < 10) {
            time = [NSString stringWithFormat:@"%@0%d:",time,year];
        }
        else
        {
            time = [NSString stringWithFormat:@"%@%d:",time,year];
        }
        
        stryear = [array objectAtIndex:4];
        year = [stryear intValue];
        
        if (year < 10) {
            time = [NSString stringWithFormat:@"%@0%d:",time,year];
        }
        else
        {
            time = [NSString stringWithFormat:@"%@%d:",time,year];
        }
        
        stryear = [array objectAtIndex:5];
        year = [stryear intValue];
        
        if (year < 10) {
            time = [NSString stringWithFormat:@"%@0%d",time,year];
        }
        else
        {
            time = [NSString stringWithFormat:@"%@%d",time,year];
        }
    }
    
    return time;
}

-(void)SortCommentArr
{
    self.commentArr = [NSMutableArray arrayWithArray:
    [self.commentArr sortedArrayUsingFunction:customSortFCC context:nil]];
}

NSInteger customSortFCC(id obj1, id obj2,void* context)
{
    NSString* obj_1 = (NSString*)obj1;
    NSString* obj_2 = (NSString*)obj2;
    
    NSComparisonResult res = (NSComparisonResult)NSOrderedSame;
    
    do {
        
        FirCirCommentInfo *fs1 = [[DataControl shareControl].fccommentDS getCommentInfoByID:obj_1];
        FirCirCommentInfo *fs2 = [[DataControl shareControl].fccommentDS getCommentInfoByID:obj_2];
        
        if (fs1 == nil || fs2 == nil) {
            break;
        }
        
        obj_1 = fs1.strtime;
        obj_2 = fs2.strtime;
        
        NSArray *array = [fs1.strtime componentsSeparatedByString:@"/"];
        NSArray *array1 = [fs2.strtime componentsSeparatedByString:@"/"];
        
        if ([array count] != 6 || [array1 count] != 6) {
            break;
        }
        
        for(int i = 0 ; i < 6 ; ++i)
        {
            int idate = [[array objectAtIndex:i] intValue];
            int idate1 = [[array1 objectAtIndex:i] intValue];
            
            if(idate > idate1)
            {
                res = (NSComparisonResult)NSOrderedAscending;
                break;
            }
            else if(idate < idate1)
            {
                res = (NSComparisonResult)NSOrderedDescending;
                break;
            }
        }
        
    } while (false);
    
    return res;
}

@end
